Namespacing everything to /UVa.
[andmenj-acm.git] / UVa / 11448 - Who said crisis / bigint-2007.07.07 / BigUnsigned.cc
blob3c9a1d7172825581867e7461b44637d3a410a6ea
1 /*
2 * Matt McCutchen's Big Integer Library
3 */
5 #include "BigUnsigned.hh"
7 // The "management" routines that used to be here are now in NumberlikeArray.hh.
9 /*
10 * The steps for construction of a BigUnsigned
11 * from an integral value x are as follows:
12 * 1. If x is zero, create an empty BigUnsigned and stop.
13 * 2. If x is negative, throw an exception.
14 * 3. Allocate a one-block number array.
15 * 4. If x is of a signed type, convert x to the unsigned
16 * type of the same length.
17 * 5. Expand x to a Blk, and store it in the number array.
19 * Since 2005.01.06, NumberlikeArray uses `NULL' rather
20 * than a real array if one of zero length is needed.
21 * These constructors implicitly call NumberlikeArray's
22 * default constructor, which sets `blk = NULL, cap = len = 0'.
23 * So if the input number is zero, they can just return.
24 * See remarks in `NumberlikeArray.hh'.
27 BigUnsigned::BigUnsigned(unsigned long x) {
28 if (x == 0)
29 ; // NumberlikeArray already did all the work
30 else {
31 cap = 1;
32 blk = new Blk[1];
33 len = 1;
34 blk[0] = Blk(x);
38 BigUnsigned::BigUnsigned(long x) {
39 if (x == 0)
41 else if (x > 0) {
42 cap = 1;
43 blk = new Blk[1];
44 len = 1;
45 blk[0] = Blk(x);
46 } else
47 throw "BigUnsigned::BigUnsigned(long): Cannot construct a BigUnsigned from a negative number";
50 BigUnsigned::BigUnsigned(unsigned int x) {
51 if (x == 0)
53 else {
54 cap = 1;
55 blk = new Blk[1];
56 len = 1;
57 blk[0] = Blk(x);
61 BigUnsigned::BigUnsigned(int x) {
62 if (x == 0)
64 else if (x > 0) {
65 cap = 1;
66 blk = new Blk[1];
67 len = 1;
68 blk[0] = Blk(x);
69 } else
70 throw "BigUnsigned::BigUnsigned(int): Cannot construct a BigUnsigned from a negative number";
73 BigUnsigned::BigUnsigned(unsigned short x) {
74 if (x == 0)
76 else {
77 cap = 1;
78 blk = new Blk[1];
79 len = 1;
80 blk[0] = Blk(x);
84 BigUnsigned::BigUnsigned(short x) {
85 if (x == 0)
87 else if (x > 0) {
88 cap = 1;
89 blk = new Blk[1];
90 len = 1;
91 blk[0] = Blk(x);
92 } else
93 throw "BigUnsigned::BigUnsigned(short): Cannot construct a BigUnsigned from a negative number";
96 // CONVERTERS
98 * The steps for conversion of a BigUnsigned to an
99 * integral type are as follows:
100 * 1. If the BigUnsigned is zero, return zero.
101 * 2. If it is more than one block long or its lowest
102 * block has bits set out of the range of the target
103 * type, throw an exception.
104 * 3. Otherwise, convert the lowest block to the
105 * target type and return it.
108 namespace {
109 // These masks are used to test whether a Blk has bits
110 // set out of the range of a smaller integral type. Note
111 // that this range is not considered to include the sign bit.
112 const BigUnsigned::Blk lMask = ~0 >> 1;
113 const BigUnsigned::Blk uiMask = (unsigned int)(~0);
114 const BigUnsigned::Blk iMask = uiMask >> 1;
115 const BigUnsigned::Blk usMask = (unsigned short)(~0);
116 const BigUnsigned::Blk sMask = usMask >> 1;
119 BigUnsigned::operator unsigned long() const {
120 if (len == 0)
121 return 0;
122 else if (len == 1)
123 return (unsigned long) blk[0];
124 else
125 throw "BigUnsigned::operator unsigned long: Value is too big for an unsigned long";
128 BigUnsigned::operator long() const {
129 if (len == 0)
130 return 0;
131 else if (len == 1 && (blk[0] & lMask) == blk[0])
132 return (long) blk[0];
133 else
134 throw "BigUnsigned::operator long: Value is too big for a long";
137 BigUnsigned::operator unsigned int() const {
138 if (len == 0)
139 return 0;
140 else if (len == 1 && (blk[0] & uiMask) == blk[0])
141 return (unsigned int) blk[0];
142 else
143 throw "BigUnsigned::operator unsigned int: Value is too big for an unsigned int";
146 BigUnsigned::operator int() const {
147 if (len == 0)
148 return 0;
149 else if (len == 1 && (blk[0] & iMask) == blk[0])
150 return (int) blk[0];
151 else
152 throw "BigUnsigned::operator int: Value is too big for an int";
155 BigUnsigned::operator unsigned short() const {
156 if (len == 0)
157 return 0;
158 else if (len == 1 && (blk[0] & usMask) == blk[0])
159 return (unsigned short) blk[0];
160 else
161 throw "BigUnsigned::operator unsigned short: Value is too big for an unsigned short";
164 BigUnsigned::operator short() const {
165 if (len == 0)
166 return 0;
167 else if (len == 1 && (blk[0] & sMask) == blk[0])
168 return (short) blk[0];
169 else
170 throw "BigUnsigned::operator short: Value is too big for a short";
173 // COMPARISON
174 BigUnsigned::CmpRes BigUnsigned::compareTo(const BigUnsigned &x) const {
175 // A bigger length implies a bigger number.
176 if (len < x.len)
177 return less;
178 else if (len > x.len)
179 return greater;
180 else {
181 // Compare blocks one by one from left to right.
182 Index i = len;
183 while (i > 0) {
184 i--;
185 if (blk[i] == x.blk[i])
186 continue;
187 else if (blk[i] > x.blk[i])
188 return greater;
189 else
190 return less;
192 // If no blocks differed, the numbers are equal.
193 return equal;
197 // PUT-HERE OPERATIONS
200 * Below are implementations of the four basic arithmetic operations
201 * for `BigUnsigned's. Their purpose is to use a mechanism that can
202 * calculate the sum, difference, product, and quotient/remainder of
203 * two individual blocks in order to calculate the sum, difference,
204 * product, and quotient/remainder of two multi-block BigUnsigned
205 * numbers.
207 * As alluded to in the comment before class `BigUnsigned',
208 * these algorithms bear a remarkable similarity (in purpose, if
209 * not in implementation) to the way humans operate on big numbers.
210 * The built-in `+', `-', `*', `/' and `%' operators are analogous
211 * to elementary-school ``math facts'' and ``times tables''; the
212 * four routines below are analogous to ``long division'' and its
213 * relatives. (Only a computer can ``memorize'' a times table with
214 * 18446744073709551616 entries! (For 32-bit blocks.))
216 * The discovery of these four algorithms, called the ``classical
217 * algorithms'', marked the beginning of the study of computer science.
218 * See Section 4.3.1 of Knuth's ``The Art of Computer Programming''.
222 * On most calls to put-here operations, it's safe to read the inputs little by
223 * little and write the outputs little by little. However, if one of the
224 * inputs is coming from the same variable into which the output is to be
225 * stored (an "aliased" call), we risk overwriting the input before we read it.
226 * In this case, we first compute the result into a temporary BigUnsigned
227 * variable and then copy it into the requested output variable *this.
228 * Each put-here operation uses the DTRT_ALIASED macro (Do The Right Thing on
229 * aliased calls) to generate code for this check.
231 * I adopted this approach on 2007.02.13 (see Assignment Operators in
232 * BigUnsigned.hh). Before then, put-here operations rejected aliased calls
233 * with an exception. I think doing the right thing is better.
235 * Some of the put-here operations can probably handle aliased calls safely
236 * without the extra copy because (for example) they process blocks strictly
237 * right-to-left. At some point I might determine which ones don't need the
238 * copy, but my reasoning would need to be verified very carefully. For now
239 * I'll leave in the copy.
241 #define DTRT_ALIASED(cond, op) \
242 if (cond) { \
243 BigUnsigned tmpThis; \
244 tmpThis.op; \
245 *this = tmpThis; \
246 return; \
249 // Addition
250 void BigUnsigned::add(const BigUnsigned &a, const BigUnsigned &b) {
251 DTRT_ALIASED(this == &a || this == &b, add(a, b));
252 // If one argument is zero, copy the other.
253 if (a.len == 0) {
254 operator =(b);
255 return;
256 } else if (b.len == 0) {
257 operator =(a);
258 return;
260 // Some variables...
261 // Carries in and out of an addition stage
262 bool carryIn, carryOut;
263 Blk temp;
264 Index i;
265 // a2 points to the longer input, b2 points to the shorter
266 const BigUnsigned *a2, *b2;
267 if (a.len >= b.len) {
268 a2 = &a;
269 b2 = &b;
270 } else {
271 a2 = &b;
272 b2 = &a;
274 // Set prelimiary length and make room in this BigUnsigned
275 len = a2->len + 1;
276 allocate(len);
277 // For each block index that is present in both inputs...
278 for (i = 0, carryIn = false; i < b2->len; i++) {
279 // Add input blocks
280 temp = a2->blk[i] + b2->blk[i];
281 // If a rollover occurred, the result is less than either input.
282 // This test is used many times in the BigUnsigned code.
283 carryOut = (temp < a2->blk[i]);
284 // If a carry was input, handle it
285 if (carryIn) {
286 temp++;
287 carryOut |= (temp == 0);
289 blk[i] = temp; // Save the addition result
290 carryIn = carryOut; // Pass the carry along
292 // If there is a carry left over, increase blocks until
293 // one does not roll over.
294 for (; i < a2->len && carryIn; i++) {
295 temp = a2->blk[i] + 1;
296 carryIn = (temp == 0);
297 blk[i] = temp;
299 // If the carry was resolved but the larger number
300 // still has blocks, copy them over.
301 for (; i < a2->len; i++)
302 blk[i] = a2->blk[i];
303 // Set the extra block if there's still a carry, decrease length otherwise
304 if (carryIn)
305 blk[i] = 1;
306 else
307 len--;
310 // Subtraction
311 void BigUnsigned::subtract(const BigUnsigned &a, const BigUnsigned &b) {
312 DTRT_ALIASED(this == &a || this == &b, subtract(a, b));
313 // If b is zero, copy a. If a is shorter than b, the result is negative.
314 if (b.len == 0) {
315 operator =(a);
316 return;
317 } else if (a.len < b.len)
318 throw "BigUnsigned::subtract: Negative result in unsigned calculation";
319 // Some variables...
320 bool borrowIn, borrowOut;
321 Blk temp;
322 Index i;
323 // Set preliminary length and make room
324 len = a.len;
325 allocate(len);
326 // For each block index that is present in both inputs...
327 for (i = 0, borrowIn = false; i < b.len; i++) {
328 temp = a.blk[i] - b.blk[i];
329 // If a reverse rollover occurred, the result is greater than the block from a.
330 borrowOut = (temp > a.blk[i]);
331 // Handle an incoming borrow
332 if (borrowIn) {
333 borrowOut |= (temp == 0);
334 temp--;
336 blk[i] = temp; // Save the subtraction result
337 borrowIn = borrowOut; // Pass the borrow along
339 // If there is a borrow left over, decrease blocks until
340 // one does not reverse rollover.
341 for (; i < a.len && borrowIn; i++) {
342 borrowIn = (a.blk[i] == 0);
343 blk[i] = a.blk[i] - 1;
345 // If there's still a borrow, the result is negative.
346 // Throw an exception, but zero out this object first just in case.
347 if (borrowIn) {
348 len = 0;
349 throw "BigUnsigned::subtract: Negative result in unsigned calculation";
350 } else // Copy over the rest of the blocks
351 for (; i < a.len; i++)
352 blk[i] = a.blk[i];
353 // Zap leading zeros
354 zapLeadingZeros();
358 * About the multiplication and division algorithms:
360 * I searched unsucessfully for fast built-in operations like the `b_0'
361 * and `c_0' Knuth describes in Section 4.3.1 of ``The Art of Computer
362 * Programming'' (replace `place' by `Blk'):
364 * ``b_0[:] multiplication of a one-place integer by another one-place
365 * integer, giving a two-place answer;
367 * ``c_0[:] division of a two-place integer by a one-place integer,
368 * provided that the quotient is a one-place integer, and yielding
369 * also a one-place remainder.''
371 * I also missed his note that ``[b]y adjusting the word size, if
372 * necessary, nearly all computers will have these three operations
373 * available'', so I gave up on trying to use algorithms similar to his.
374 * A future version of the library might include such algorithms; I
375 * would welcome contributions from others for this.
377 * I eventually decided to use bit-shifting algorithms. To multiply `a'
378 * and `b', we zero out the result. Then, for each `1' bit in `a', we
379 * shift `b' left the appropriate amount and add it to the result.
380 * Similarly, to divide `a' by `b', we shift `b' left varying amounts,
381 * repeatedly trying to subtract it from `a'. When we succeed, we note
382 * the fact by setting a bit in the quotient. While these algorithms
383 * have the same O(n^2) time complexity as Knuth's, the ``constant factor''
384 * is likely to be larger.
386 * Because I used these algorithms, which require single-block addition
387 * and subtraction rather than single-block multiplication and division,
388 * the innermost loops of all four routines are very similar. Study one
389 * of them and all will become clear.
393 * This is a little inline function used by both the multiplication
394 * routine and the division routine.
396 * `getShiftedBlock' returns the `x'th block of `num << y'.
397 * `y' may be anything from 0 to N - 1, and `x' may be anything from
398 * 0 to `num.len'.
400 * Two things contribute to this block:
402 * (1) The `N - y' low bits of `num.blk[x]', shifted `y' bits left.
404 * (2) The `y' high bits of `num.blk[x-1]', shifted `N - y' bits right.
406 * But we must be careful if `x == 0' or `x == num.len', in
407 * which case we should use 0 instead of (2) or (1), respectively.
409 * If `y == 0', then (2) contributes 0, as it should. However,
410 * in some computer environments, for a reason I cannot understand,
411 * `a >> b' means `a >> (b % N)'. This means `num.blk[x-1] >> (N - y)'
412 * will return `num.blk[x-1]' instead of the desired 0 when `y == 0';
413 * the test `y == 0' handles this case specially.
415 inline BigUnsigned::Blk getShiftedBlock(const BigUnsigned &num,
416 BigUnsigned::Index x, unsigned int y) {
417 BigUnsigned::Blk part1 = (x == 0 || y == 0) ? 0 : (num.blk[x - 1] >> (BigUnsigned::N - y));
418 BigUnsigned::Blk part2 = (x == num.len) ? 0 : (num.blk[x] << y);
419 return part1 | part2;
422 // Multiplication
423 void BigUnsigned::multiply(const BigUnsigned &a, const BigUnsigned &b) {
424 DTRT_ALIASED(this == &a || this == &b, multiply(a, b));
425 // If either a or b is zero, set to zero.
426 if (a.len == 0 || b.len == 0) {
427 len = 0;
428 return;
431 * Overall method:
433 * Set this = 0.
434 * For each 1-bit of `a' (say the `i2'th bit of block `i'):
435 * Add `b << (i blocks and i2 bits)' to *this.
437 // Variables for the calculation
438 Index i, j, k;
439 unsigned int i2;
440 Blk temp;
441 bool carryIn, carryOut;
442 // Set preliminary length and make room
443 len = a.len + b.len;
444 allocate(len);
445 // Zero out this object
446 for (i = 0; i < len; i++)
447 blk[i] = 0;
448 // For each block of the first number...
449 for (i = 0; i < a.len; i++) {
450 // For each 1-bit of that block...
451 for (i2 = 0; i2 < N; i2++) {
452 if ((a.blk[i] & (Blk(1) << i2)) == 0)
453 continue;
455 * Add b to this, shifted left i blocks and i2 bits.
456 * j is the index in b, and k = i + j is the index in this.
458 * `getShiftedBlock', a short inline function defined above,
459 * is now used for the bit handling. It replaces the more
460 * complex `bHigh' code, in which each run of the loop dealt
461 * immediately with the low bits and saved the high bits to
462 * be picked up next time. The last run of the loop used to
463 * leave leftover high bits, which were handled separately.
464 * Instead, this loop runs an additional time with j == b.len.
465 * These changes were made on 2005.01.11.
467 for (j = 0, k = i, carryIn = false; j <= b.len; j++, k++) {
469 * The body of this loop is very similar to the body of the first loop
470 * in `add', except that this loop does a `+=' instead of a `+'.
472 temp = blk[k] + getShiftedBlock(b, j, i2);
473 carryOut = (temp < blk[k]);
474 if (carryIn) {
475 temp++;
476 carryOut |= (temp == 0);
478 blk[k] = temp;
479 carryIn = carryOut;
481 // No more extra iteration to deal with `bHigh'.
482 // Roll-over a carry as necessary.
483 for (; carryIn; k++) {
484 blk[k]++;
485 carryIn = (blk[k] == 0);
489 // Zap possible leading zero
490 if (blk[len - 1] == 0)
491 len--;
495 * DIVISION WITH REMAINDER
496 * The functionality of divide, modulo, and %= is included in this one monstrous call,
497 * which deserves some explanation.
499 * The division *this / b is performed.
500 * Afterwards, q has the quotient, and *this has the remainder.
501 * Thus, a call is like q = *this / b, *this %= b.
503 * This seemingly bizarre pattern of inputs and outputs has a justification. The
504 * ``put-here operations'' are supposed to be fast. Therefore, they accept inputs
505 * and provide outputs in the most convenient places so that no value ever needs
506 * to be copied in its entirety. That way, the client can perform exactly the
507 * copying it needs depending on where the inputs are and where it wants the output.
508 * A better name for this function might be "modWithQuotient", but I would rather
509 * not change the name now.
511 void BigUnsigned::divideWithRemainder(const BigUnsigned &b, BigUnsigned &q) {
513 * Defending against aliased calls is a bit tricky because we are
514 * writing to both *this and q.
516 * It would be silly to try to write quotient and remainder to the
517 * same variable. Rule that out right away.
519 if (this == &q)
520 throw "BigUnsigned::divideWithRemainder: Cannot write quotient and remainder into the same variable";
522 * Now *this and q are separate, so the only concern is that b might be
523 * aliased to one of them. If so, use a temporary copy of b.
525 if (this == &b || &q == &b) {
526 BigUnsigned tmpB(b);
527 divideWithRemainder(tmpB, q);
528 return;
532 * Note that the mathematical definition of mod (I'm trusting Knuth) is somewhat
533 * different from the way the normal C++ % operator behaves in the case of division by 0.
534 * This function does it Knuth's way.
536 * We let a / 0 == 0 (it doesn't matter) and a % 0 == a, no exceptions thrown.
537 * This allows us to preserve both Knuth's demand that a mod 0 == a
538 * and the useful property that (a / b) * b + (a % b) == a.
540 if (b.len == 0) {
541 q.len = 0;
542 return;
546 * If *this.len < b.len, then *this < b, and we can be sure that b doesn't go into
547 * *this at all. The quotient is 0 and *this is already the remainder (so leave it alone).
549 if (len < b.len) {
550 q.len = 0;
551 return;
555 * At this point we know *this > b > 0. (Whew!)
559 * Overall method:
561 * For each appropriate i and i2, decreasing:
562 * Try to subtract (b << (i blocks and i2 bits)) from *this.
563 * (`work2' holds the result of this subtraction.)
564 * If the result is nonnegative:
565 * Turn on bit i2 of block i of the quotient q.
566 * Save the result of the subtraction back into *this.
567 * Otherwise:
568 * Bit i2 of block i remains off, and *this is unchanged.
570 * Eventually q will contain the entire quotient, and *this will
571 * be left with the remainder.
573 * We use work2 to temporarily store the result of a subtraction.
574 * work2[x] corresponds to blk[x], not blk[x+i], since 2005.01.11.
575 * If the subtraction is successful, we copy work2 back to blk.
576 * (There's no `work1'. In a previous version, when division was
577 * coded for a read-only dividend, `work1' played the role of
578 * the here-modifiable `*this' and got the remainder.)
580 * We never touch the i lowest blocks of either blk or work2 because
581 * they are unaffected by the subtraction: we are subtracting
582 * (b << (i blocks and i2 bits)), which ends in at least `i' zero blocks.
584 // Variables for the calculation
585 Index i, j, k;
586 unsigned int i2;
587 Blk temp;
588 bool borrowIn, borrowOut;
591 * Make sure we have an extra zero block just past the value.
593 * When we attempt a subtraction, we might shift `b' so
594 * its first block begins a few bits left of the dividend,
595 * and then we'll try to compare these extra bits with
596 * a nonexistent block to the left of the dividend. The
597 * extra zero block ensures sensible behavior; we need
598 * an extra block in `work2' for exactly the same reason.
600 * See below `divideWithRemainder' for the interesting and
601 * amusing story of this section of code.
603 Index origLen = len; // Save real length.
604 // 2006.05.03: Copy the number and then change the length!
605 allocateAndCopy(len + 1); // Get the space.
606 len++; // Increase the length.
607 blk[origLen] = 0; // Zero the extra block.
609 // work2 holds part of the result of a subtraction; see above.
610 Blk *work2 = new Blk[len];
612 // Set preliminary length for quotient and make room
613 q.len = origLen - b.len + 1;
614 q.allocate(q.len);
615 // Zero out the quotient
616 for (i = 0; i < q.len; i++)
617 q.blk[i] = 0;
619 // For each possible left-shift of b in blocks...
620 i = q.len;
621 while (i > 0) {
622 i--;
623 // For each possible left-shift of b in bits...
624 // (Remember, N is the number of bits in a Blk.)
625 q.blk[i] = 0;
626 i2 = N;
627 while (i2 > 0) {
628 i2--;
630 * Subtract b, shifted left i blocks and i2 bits, from *this,
631 * and store the answer in work2. In the for loop, `k == i + j'.
633 * Compare this to the middle section of `multiply'. They
634 * are in many ways analogous. See especially the discussion
635 * of `getShiftedBlock'.
637 for (j = 0, k = i, borrowIn = false; j <= b.len; j++, k++) {
638 temp = blk[k] - getShiftedBlock(b, j, i2);
639 borrowOut = (temp > blk[k]);
640 if (borrowIn) {
641 borrowOut |= (temp == 0);
642 temp--;
644 // Since 2005.01.11, indices of `work2' directly match those of `blk', so use `k'.
645 work2[k] = temp;
646 borrowIn = borrowOut;
648 // No more extra iteration to deal with `bHigh'.
649 // Roll-over a borrow as necessary.
650 for (; k < origLen && borrowIn; k++) {
651 borrowIn = (blk[k] == 0);
652 work2[k] = blk[k] - 1;
655 * If the subtraction was performed successfully (!borrowIn),
656 * set bit i2 in block i of the quotient.
658 * Then, copy the portion of work2 filled by the subtraction
659 * back to *this. This portion starts with block i and ends--
660 * where? Not necessarily at block `i + b.len'! Well, we
661 * increased k every time we saved a block into work2, so
662 * the region of work2 we copy is just [i, k).
664 if (!borrowIn) {
665 q.blk[i] |= (Blk(1) << i2);
666 while (k > i) {
667 k--;
668 blk[k] = work2[k];
673 // Zap possible leading zero in quotient
674 if (q.blk[q.len - 1] == 0)
675 q.len--;
676 // Zap any/all leading zeros in remainder
677 zapLeadingZeros();
678 // Deallocate temporary array.
679 // (Thanks to Brad Spencer for noticing my accidental omission of this!)
680 delete [] work2;
684 * The out-of-bounds accesses story:
686 * On 2005.01.06 or 2005.01.07 (depending on your time zone),
687 * Milan Tomic reported out-of-bounds memory accesses in
688 * the Big Integer Library. To investigate the problem, I
689 * added code to bounds-check every access to the `blk' array
690 * of a `NumberlikeArray'.
692 * This gave me warnings that fell into two categories of false
693 * positives. The bounds checker was based on length, not
694 * capacity, and in two places I had accessed memory that I knew
695 * was inside the capacity but that wasn't inside the length:
697 * (1) The extra zero block at the left of `*this'. Earlier
698 * versions said `allocateAndCopy(len + 1); blk[len] = 0;'
699 * but did not increment `len'.
701 * (2) The entire digit array in the conversion constructor
702 * ``BigUnsignedInABase(BigUnsigned)''. It was allocated with
703 * a conservatively high capacity, but the length wasn't set
704 * until the end of the constructor.
706 * To simplify matters, I changed both sections of code so that
707 * all accesses occurred within the length. The messages went
708 * away, and I told Milan that I couldn't reproduce the problem,
709 * sending a development snapshot of the bounds-checked code.
711 * Then, on 2005.01.09-10, he told me his debugger still found
712 * problems, specifically at the line `delete [] work2'.
713 * It was `work2', not `blk', that was causing the problems;
714 * this possibility had not occurred to me at all. In fact,
715 * the problem was that `work2' needed an extra block just
716 * like `*this'. Go ahead and laugh at me for finding (1)
717 * without seeing what was actually causing the trouble. :-)
719 * The 2005.01.11 version fixes this problem. I hope this is
720 * the last of my memory-related bloopers. So this is what
721 * starts happening to your C++ code if you use Java too much!
724 // Bitwise and
725 void BigUnsigned::bitAnd(const BigUnsigned &a, const BigUnsigned &b) {
726 DTRT_ALIASED(this == &a || this == &b, bitAnd(a, b));
727 len = (a.len >= b.len) ? b.len : a.len;
728 allocate(len);
729 Index i;
730 for (i = 0; i < len; i++)
731 blk[i] = a.blk[i] & b.blk[i];
732 zapLeadingZeros();
735 // Bitwise or
736 void BigUnsigned::bitOr(const BigUnsigned &a, const BigUnsigned &b) {
737 DTRT_ALIASED(this == &a || this == &b, bitOr(a, b));
738 Index i;
739 const BigUnsigned *a2, *b2;
740 if (a.len >= b.len) {
741 a2 = &a;
742 b2 = &b;
743 } else {
744 a2 = &b;
745 b2 = &a;
747 allocate(a2->len);
748 for (i = 0; i < b2->len; i++)
749 blk[i] = a2->blk[i] | b2->blk[i];
750 for (; i < a2->len; i++)
751 blk[i] = a2->blk[i];
752 len = a2->len;
755 // Bitwise xor
756 void BigUnsigned::bitXor(const BigUnsigned &a, const BigUnsigned &b) {
757 DTRT_ALIASED(this == &a || this == &b, bitXor(a, b));
758 Index i;
759 const BigUnsigned *a2, *b2;
760 if (a.len >= b.len) {
761 a2 = &a;
762 b2 = &b;
763 } else {
764 a2 = &b;
765 b2 = &a;
767 allocate(a2->len);
768 for (i = 0; i < b2->len; i++)
769 blk[i] = a2->blk[i] ^ b2->blk[i];
770 for (; i < a2->len; i++)
771 blk[i] = a2->blk[i];
772 len = a2->len;
773 zapLeadingZeros();
776 // Bitwise shift left
777 void BigUnsigned::bitShiftLeft(const BigUnsigned &a, unsigned int b) {
778 DTRT_ALIASED(this == &a, bitShiftLeft(a, b));
779 Index shiftBlocks = b / N;
780 unsigned int shiftBits = b % N;
781 // + 1: room for high bits nudged left into another block
782 len = a.len + shiftBlocks + 1;
783 allocate(len);
784 Index i, j;
785 for (i = 0; i < shiftBlocks; i++)
786 blk[i] = 0;
787 for (j = 0, i = shiftBlocks; j <= a.len; j++, i++)
788 blk[i] = getShiftedBlock(a, j, shiftBits);
789 // Zap possible leading zero
790 if (blk[len - 1] == 0)
791 len--;
794 // Bitwise shift right
795 void BigUnsigned::bitShiftRight(const BigUnsigned &a, unsigned int b) {
796 DTRT_ALIASED(this == &a, bitShiftRight(a, b));
797 // This calculation is wacky, but expressing the shift as a left bit shift
798 // within each block lets us use getShiftedBlock.
799 Index rightShiftBlocks = (b + N - 1) / N;
800 unsigned int leftShiftBits = N * rightShiftBlocks - b;
801 // Now (N * rightShiftBlocks - leftShiftBits) == b
802 // and 0 <= leftShiftBits < N.
803 if (rightShiftBlocks >= a.len + 1) {
804 // All of a is guaranteed to be shifted off, even considering the left
805 // bit shift.
806 len = 0;
807 return;
809 // Now we're allocating a positive amount.
810 // + 1: room for high bits nudged left into another block
811 len = a.len + 1 - rightShiftBlocks;
812 allocate(len);
813 Index i, j;
814 for (j = rightShiftBlocks, i = 0; j <= a.len; j++, i++)
815 blk[i] = getShiftedBlock(a, j, leftShiftBits);
816 // Zap possible leading zero
817 if (blk[len - 1] == 0)
818 len--;
821 // INCREMENT/DECREMENT OPERATORS
823 // Prefix increment
824 void BigUnsigned::operator ++() {
825 Index i;
826 bool carry = true;
827 for (i = 0; i < len && carry; i++) {
828 blk[i]++;
829 carry = (blk[i] == 0);
831 if (carry) {
832 // Matt fixed a bug 2004.12.24: next 2 lines used to say allocateAndCopy(len + 1)
833 // Matt fixed another bug 2006.04.24:
834 // old number only has len blocks, so copy before increasing length
835 allocateAndCopy(len + 1);
836 len++;
837 blk[i] = 1;
841 // Postfix increment: same as prefix
842 void BigUnsigned::operator ++(int) {
843 operator ++();
846 // Prefix decrement
847 void BigUnsigned::operator --() {
848 if (len == 0)
849 throw "BigUnsigned::operator --(): Cannot decrement an unsigned zero";
850 Index i;
851 bool borrow = true;
852 for (i = 0; borrow; i++) {
853 borrow = (blk[i] == 0);
854 blk[i]--;
856 // Zap possible leading zero (there can only be one)
857 if (blk[len - 1] == 0)
858 len--;
861 // Postfix decrement: same as prefix
862 void BigUnsigned::operator --(int) {
863 operator --();